package com.apes.fico.masterdata.gl.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.metadata.BusinessField;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;

/**
 * @program: apestech
 * @description: 科目辅助核算项目
 * @author: Chengxm
 * @create: 2018-12-20 14:42
 **/

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = AccountingAuxiliaryProject.CONST_TABLE_NAME)
public class AccountingAuxiliaryProject extends Model {
    public final static String CONST_TABLE_NAME = "mas_acc_auxiliary_project";

    @Id
    @Column
    @ApesGenerator(code = CONST_TABLE_NAME, name = "科目辅助项目")
    @Comment("辅助核算项编码（属性名)")
    private String id;

    @Comment("辅助核算项名称")
    @Column(name = "name", nullable = false, length = 64)
    private String name;

    @Comment("数据来源")
    @Column(name = "api", nullable = false, length = 64)
    private String api;

    @Comment("业务属性字段")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_field", referencedColumnName = "id", nullable = false)
    private BusinessField businessField;


    @Column(name = "value_formatter", length = 64)
    @Comment("显示格式化")
    private String valueFormatter;

    @Column(name = "domain", length = 400)
    @Comment("前置条件")
    private String domain = "";

    @Column(name = "repository", length = 400)
    @Comment("持久类")
    private String repository = "";

    @Column(name = "cash_flow",columnDefinition = "number(1,0) default 0")
    @Comment("现金流量")//0:否 1：是
    private boolean cashFlow = false;

    /**
     * 取业务模型通用字段名
     **/
    public String commonName(String businessTypeId) {
        if (!StringUtils.isEmpty(businessTypeId)) {
            String code = businessField.businessTypeCode(businessTypeId);
            if (code != null) return code;
        }
        return businessField.getId();
    }


}
